package com.li.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Author: Li
 * Date: 2017/3/29
 * Description: BaseFragment
 */

public abstract class BaseFragment extends Fragment {


    public View getContentView() {
        return mContentView;
    }

    protected View mContentView;
    private boolean mIsFirst = true;
    protected Context mContext;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mContentView = inflater.inflate(getLayoutId(), container, false);
        initSth();
        onCreate();
        return mContentView;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        if (mContentView != null) {
            if (mIsFirst && isVisibleToUser) {
                mIsFirst = false;
                onFirstVisible();
            }
            if (isVisibleToUser) {
                onVisible();
            }

            if (!isVisibleToUser) {
                onInvisible();
            }
        }
    }

    /**
     * get the fragment's view's id
     *
     * @return resId of the view what the fragment show
     */
    protected abstract int getLayoutId();

    protected abstract void initSth();

    /**
     * when the fragment is visible
     */
    protected void onVisible() {
    }

    /**
     * when the fragment is invisible
     */
    protected void onInvisible() {
    }

    /**
     * when the fragment is created
     */
    protected void onCreate() {
    }

    /**
     * when the fragment is first time to be visible
     */
    protected void onFirstVisible() {
    }


}